package util;

import java.lang.reflect.Method;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 
 *
 */
public class DBUtil {

	/**
	 * 解析执行sql结果resultSet中数据
	 * 
	 * @author sun.mengya
	 * @param resultSet
	 * @return 解析后的数据
	 * @throws SQLException 
	 * @throws Exception
	 */
	public static List<Map<String, Object>> analyzeResultSet(ResultSet resultSet) throws SQLException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> mapArray = new ArrayList<Map<String, Object>>();
		ResultSetMetaData metaData = resultSet.getMetaData();
		while (resultSet.next()) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			for (int i = 0; i < metaData.getColumnCount(); i++) {
				int columnType = metaData.getColumnType(i + 1);
				String columnName = metaData.getColumnLabel(i + 1);
				switch (columnType) {
				case Types.TIMESTAMP:
					Timestamp timeStamp = resultSet.getTimestamp(columnName);
					if(timeStamp!=null){
						dataMap.put(columnName, timeFormat.format(new java.util.Date(timeStamp.getTime())));
					}else{
						dataMap.put(columnName,"");
					}
					
					break;
				case Types.DATE:
					Date date = resultSet.getDate(columnName);
					if(date!=null){
						dataMap.put(columnName, dateFormat.format(new java.util.Date(date.getTime())));
					}else{
						dataMap.put(columnName,"");
					}
					break;
				case Types.VARCHAR:
					dataMap.put(columnName, resultSet.getString(columnName));
					break;
				case Types.INTEGER:
				case Types.DECIMAL:
					dataMap.put(columnName, resultSet.getInt(columnName));
					break;
				case Types.BIGINT:
					dataMap.put(columnName, resultSet.getLong(columnName));
					break;
				case Types.TINYINT:
					dataMap.put(columnName, resultSet.getInt(columnName));
					break;
				case Types.BIT:
					dataMap.put(columnName, resultSet.getInt(columnName));
					break;
				case Types.BOOLEAN:
					dataMap.put(columnName, resultSet.getBoolean(columnName));
				default:
					dataMap.put(columnName, resultSet.getObject(columnName));
					break;
				}

			}
			mapArray.add(dataMap);
		}
		return mapArray;
	}
	
	public static <T> List<T> getObject(Class<T> t, List<Map<String, Object>> dataList) {
		List<T> list = new ArrayList<>();
		for(Map<String, Object> data:dataList){
			T object = getObject(t, data);
			list.add(object);
		}
		return list;
	}

	/**
	 * 根据map生成bean对象
	 * 
	 * @author sun.mengya
	 * @param t
	 * @param dataMap
	 * @return
	 */
	public static <T> T getObject(Class<T> t, Map<String, Object> dataMap) {

		try {
			T newInstance = t.newInstance();
			if (dataMap != null) {
				Method[] methods = newInstance.getClass().getMethods();
				Set<String> keySet = dataMap.keySet();
				for (String key : keySet) {
					
					Object value = dataMap.get(key);
					if (value == null) {
						continue;
					}
					if(key.indexOf("_")>0){
						key=key.replaceAll("_", "");
					}
					for (Method method : methods) {
						if (method.getName().equalsIgnoreCase("set" + key)) {
							try {
								method.invoke(newInstance, value);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						}
					}
				}
			}
			return newInstance;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
